package com.lewei.print;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;

import com.lewei.utils.Constants;

/**
 * OL100数据打印模型
 * @author 22901
 *
 */
public class OL100 implements Printable{
	/** 标签样式 */
	private String lenFlag="E";
	/**试验单位 */
	private  String testingAgency="ETC";
	/**日期 */
	private  String prodDatea="2017-02-09";
	
	/**编号 */
	private  String typeNo="80340G211L23SGAK-CAR-L";
	
	/**出厂编号 */
	private  String serialNo="300018_560";
	
	/**限速器名称 */
	private  String elevatorNo="OL100";
	
	/**Vns */
	private  String vns="3.50";
	
	/**Vcs */
	private  String vcs="4.03";
	
	/**Vts */
	private  String vts="4.20";
	
	/**钢丝绳粗细 */
	private  String lineThickness="10.0";
	
	/**FbrakeMin */
	private  String fbrakeMin="2100";
	
	/** 标签样式 */
	private String labelType="EN81";
	
	/**型号 */
	private String modelNo="80340";
	
	/**证书编号 */
	private String certificateNo="TUV-A-AT-1-99-002-CEGB-3";

	/** 左边距 */
	private double paddingLeft = 0;

	/** 上边距 */
	private double paddingTop = 0;

	/** 打印机名称 */
	private String printerName = "Foxit Reader Plus Printer";
	
	
	
	

	public String getTestingAgency() {
		return testingAgency;
	}


	public void setTestingAgency(String testingAgency) {
		this.testingAgency = testingAgency;
	}


	public String getLenFlag() {
		return lenFlag;
	}


	public void setLenFlag(String lenFlag) {
		this.lenFlag = lenFlag;
	}


	public String getProdDatea() {
		return prodDatea;
	}


	public void setProdDatea(String prodDatea) {
		this.prodDatea = prodDatea;
	}

	public String getTypeNo() {
		return typeNo;
	}

	public void setTypeNo(String typeNo) {
		this.typeNo = typeNo;
	}
	
	public String getSerialNo() {
		return serialNo;
	}
	
	public void setSerialNo(String serialNo) {
		this.serialNo = serialNo;
	}
	
	public String getElevatorNo() {
		return elevatorNo;
	}

	public void setElevatorNo(String elevatorNo) {
		this.elevatorNo = elevatorNo;
	}

	public String getVns() {
		return vns;
	}

	public void setVns(String vns) {
		this.vns = vns;
	}

	public String getVcs() {
		return vcs;
	}

	public void setVcs(String vcs) {
		this.vcs = vcs;
	}

	public String getVts() {
		return vts;
	}

	public void setVts(String vts) {
		this.vts = vts;
	}

	public String getLineThickness() {
		return lineThickness;
	}


	public void setLineThickness(String lineThickness) {
		this.lineThickness = lineThickness;
	}

	public String getFbrakeMin() {
		return fbrakeMin;
	}

	public void setFbrakeMin(String fbrakeMin) {
		this.fbrakeMin = fbrakeMin;
	}

	public String getLabelType() {
		return labelType;
	}


	public void setLabelType(String labelType) {
		this.labelType = labelType;
	}

	public String getModelNo() {
		return modelNo;
	}

	public void setModelNo(String modelNo) {
		this.modelNo = modelNo;
	}

	public String getCertificateNo() {
		return certificateNo;
	}

	public void setCertificateNo(String certificateNo) {
		this.certificateNo = certificateNo;
	}

	public double getPaddingLeft() {
		return paddingLeft;
	}


	public void setPaddingLeft(double paddingLeft) {
		this.paddingLeft = paddingLeft;
	}


	public double getPaddingTop() {
		return paddingTop;
	}


	public void setPaddingTop(double paddingTop) {
		this.paddingTop = paddingTop;
	}


	public String getPrinterName() {
		return printerName;
	}


	public void setPrinterName(String printerName) {
		this.printerName = printerName;
	}
	/**
	 * 设置边距。
	 * 
	 * @param paddingLeft
	 * @param paddingTop
	 */
	public void setPadding(double paddingLeft, double paddingTop) {
		this.paddingLeft = paddingLeft;
		this.paddingTop = paddingTop;
	}

	public OL100(String printerName) {
		super();
		this.printerName = printerName;
	}

	public OL100() {
		super();
	}

	/**
	 * @param Graphic指明打印的图形环境
	 * @param PageFormat指明打印页格式（页面大小以点为计量单位，1点为1英才的1/72，1英寸为25.4毫米。A4纸大致为595×
	 *            842点）
	 * @param pageIndex指明页号
	 **/

	@Override
	public int print(Graphics gra, PageFormat pf, int pageIndex)
			throws PrinterException {
		// TODO Auto-generated method stub
	         	 Component c = null;
				// 转换成Graphics2D
				Graphics2D g2 = (Graphics2D) gra;
				// 设置打印颜色为黑色
				g2.setColor(Color.black);

//				 打印起点坐标
				 double x = pf.getImageableX()+50;
				 double y = pf.getImageableY();

				switch (pageIndex) {
				case 0:
					if(lenFlag.equals("E")||lenFlag.equals("U")||lenFlag.equals("T")){
					// 设置打印字体（字体名称、样式和点大小）（字体名称可以是物理或者逻辑名称）
					// Java平台所定义的五种字体系列：Serif、SansSerif、Monospaced、Dialog 和 DialogInput
//					Font font = new Font("Calibri", Font.BOLD, Constants.LABELFONTSIZE);
//					g2.setFont(font);// 设置字体
					// BasicStroke bs_3=new BasicStroke(0.5f);
					// float[] dash1 = {2.0f};
					// 设置打印线的属性。
					// 1.线宽 2、3、不知道，4、空白的宽度，5、虚线的宽度，6、偏移量
					// g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT,
					// BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f));
					// g2.setStroke(bs_3);//设置线宽
					// float heigth = font.getSize2D();// 字体高度
					// -1- 用Graphics2D直接输出
					// 首字符的基线(右下部)位于用户空间中的 (x, y) 位置处
					// g2.drawLine(10,10,200,300);
					// Image codeImg = null;
					// try {
					// codeImg = ImageIO.read(new
					// File("C:\\toolsZ\\codeZ\\Code128.png"));
					// } catch (IOException e) {
					// e.printStackTrace();
					// throw new RuntimeException("无法加载条码！");
					// }
				     
					Font font = new Font("Calibri", Font.BOLD, 10);
					g2.setFont(font);
					g2.drawString(typeNo, (float) (x+ 15),
							(float) (y+20));
					 String filename = "C:\\toolsZ\\labelPrint\\codeZ\\typeNo.jpg";
					 Image Img = null;
					 try {
						 Img = ImageIO.read(new File(filename));
					 } catch (IOException e) {
						 // TODO Auto-generated catch block
						 e.printStackTrace();
					 }
					 g2.drawImage(Img,(int)x+130,(int)y+15,55,55,c);
					 font = new Font("Calibri", Font.BOLD, 8);
						g2.setFont(font);
					 g2.drawString(serialNo, (float) (x+ 50),
								(float) (y+55));
					 filename = "C:\\toolsZ\\labelPrint\\codeZ\\serialNo.jpg";
					 try {
						 Img = ImageIO.read(new File(filename));
					 } catch (IOException e) {
						 // TODO Auto-generated catch block
						 e.printStackTrace();
					 }
					 g2.drawImage(Img,(int)x-5,(int)y+40,55,55,c);
					 font = new Font("Calibri", Font.BOLD, 10);
						g2.setFont(font);
					 g2.drawString(elevatorNo, (float) (x+ 90),
								(float) (y+85));
					 g2.drawString(vns, (float) (x+ 118),
								(float) (y+107));
					 g2.drawString(vcs, (float) (x+ 118),
								(float) (y+127));
					 g2.drawString(vts, (float) (x+ 118),
								(float) (y+147));
					 g2.drawString(lineThickness, (float) (x+ 120),
								(float) (y+167));
					 g2.drawString(fbrakeMin, (float) (x+ 120),
								(float) (y+187));
					 g2.drawString(modelNo, (float) (x-32),
								(float) (y+90));
					font = new Font("Calibri", Font.BOLD, 5);
					 g2.setFont(font);
					 g2.drawString(prodDatea, (float) (x-32),
								(float) (y+25));
					 
					 //设置字体方向
					 g2.rotate(Math.toRadians(270));
					 font = new Font("Calibri", Font.BOLD, 9);
					 g2.setFont(font);
//					 filename = "C:\\toolsZ\\labelPrint\\codeZ\\kone.png";
//					 try {
//						 Img = ImageIO.read(new File(filename));
//					 } catch (IOException e) {
//						 // TODO Auto-generated catch block
//						 e.printStackTrace();
//					 }
//					 g2.drawImage(Img,(int)(-(y+70)),(int)(x-25),30,12,c);
					 if(lenFlag.equals("U")||lenFlag.equals("Ts")){
						 g2.drawString("EN81-20", (float) (-(y+152)),
									(float) (x+177));
					 }else{
						  g2.drawString(labelType, (float) (-(y+148)),
								(float) (x+177));
					 }
					
					 font = new Font("Calibri", Font.BOLD, 7);
					 g2.setFont(font);
					 g2.drawString(modelNo,(float) (-(y+122)),
								(float) (x+222));
					 g2.drawString(certificateNo,(float) (-(y+158)),
								(float) (x+232));
					 g2.drawString(prodDatea,(float) (-(y+130)),
								(float) (x+248));
					}else{
						//中文标签
						Font font = new Font("微软雅黑", Font.BOLD, 10);
						g2.setFont(font);
						g2.drawString(typeNo, (float) (x+ 15),
								(float) (y+18));
						 String filename = "C:\\toolsZ\\labelPrint\\codeZ\\typeNo.jpg";
						 Image Img = null;
						 try {
						  	 Img = ImageIO.read(new File(filename));
						 } catch (IOException e) {
							 // TODO Auto-generated catch block
							 e.printStackTrace();
						 }
						 g2.drawImage(Img,(int)x+120,(int)y,55,55,c);
						 font = new Font("微软雅黑", Font.BOLD, 9);
						 g2.setFont(font);
						 g2.drawString(serialNo, (float) (x+ 50),
									(float) (y+65));
						 font = new Font("微软雅黑", Font.BOLD, 10);
						 g2.setFont(font);
						 filename = "C:\\toolsZ\\labelPrint\\codeZ\\serialNo.jpg";
						 try {
							 Img = ImageIO.read(new File(filename));
						 } catch (IOException e) {
							 // TODO Auto-generated catch block
							 e.printStackTrace();
						 }
						 g2.drawImage(Img,(int)x-5,(int)y+25,55,55,c);
						
						 g2.drawString(vns, (float) (x+ 130),
									(float) (y+90));
						 g2.drawString(vcs, (float) (x+ 130),
									(float) (y+120));
						 g2.drawString(vts, (float) (x+ 130),
									(float) (y+147));
						 g2.drawString(lineThickness, (float) (x+ 130),
									(float) (y+170));
						 g2.drawString(fbrakeMin, (float) (x+ 130),
									(float) (y+191));
						 g2.drawString(modelNo, (float) (x-32),
									(float) (y+70));
						 font = new Font("Calibri", Font.BOLD, 5);
							g2.setFont(font);
						 g2.drawString(prodDatea, (float) (x-32),
									(float) (y+25));
//						 filename = "C:\\toolsZ\\labelPrint\\codeZ\\kone.png";
//						 try {
//							 Img = ImageIO.read(new File(filename));
//						 } catch (IOException e) {
//							 // TODO Auto-generated catch block
//							 e.printStackTrace();
//						 }
//						 g2.drawImage(Img,(int)x-34,(int)y+40,30,12,c);
						 
						 
						  font = new Font("Calibri", Font.BOLD, 9);
							g2.setFont(font);
						 g2.drawString(modelNo, (float) (x+ 220),
									(float) (y+45));
						 g2.drawString(testingAgency, (float) (x+ 220),
									(float) (y+105));
						 g2.drawString("TSX", (float) (x+ 220),
									(float) (y+75));
						 
					}
				
					return PAGE_EXISTS;
				default:
					return NO_SUCH_PAGE;
				}
	}
	
	public static void main(String[] args) {

		// 通俗理解就是书、文档
		Book book = new Book();
		// 设置成竖打
		PageFormat pf = new PageFormat();
		pf.setOrientation(PageFormat.PORTRAIT);
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();
		p.setSize(800, 600);// 纸张大小
		// A4(595 X 842)设置打印区域，其实0，0应该是72，72，因为A4纸的默认X,Y边距是72
		p.setImageableArea(0, 0, 800, 600);
		pf.setPaper(p);
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(new OL100(), pf);

		// 获取打印服务对象
		PrinterJob job = PrinterJob.getPrinterJob();

		HashAttributeSet hs = new HashAttributeSet();
		
		//String printerName = "HP LaserJet Professional P1106";
		String printerName = "Microsoft Print to PDF";
		hs.add(new PrinterName(printerName, null));

		PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, hs);

		try {
			job.setPrintService(pss[0]);
		} catch (PrinterException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// 设置打印类
		job.setPageable(book);

		try {
			// 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印
			// boolean a=job.printDialog();
			// if(a)
			// {
			job.print();
			// }
		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}
	
	public void printcode() {

		// 通俗理解就是书、文档
		Book book = new Book();

		PageFormat pageFormat = new PageFormat();
		// 打印方向
		pageFormat.setOrientation(PageFormat.PORTRAIT);
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper paper = new Paper();
		paper.setSize(800, 600);// 纸张大小
		if(lenFlag.equals("E")||lenFlag.equals("U")){
			// A4(595 X 842)设置打印区域，其实0，0应该是72，72，因为A4纸的默认X,Y边距是72
			paper.setImageableArea(Constants.LABELLEFT80340E, Constants.LABELTOP80340E,  800, 600);
		}else{
			paper.setImageableArea(Constants.LABELLEFT80340, Constants.LABELTOP80340,  800, 600);
		}
		
		pageFormat.setPaper(paper);
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(this, pageFormat);

		// 获取打印服务对象
		PrinterJob printJob = PrinterJob.getPrinterJob();

		HashAttributeSet hashAttributeSet = new HashAttributeSet();

		hashAttributeSet.add(new PrinterName(printerName, null));

		PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, hashAttributeSet);

		try {
			printJob.setPrintService(printServices[0]);
		} catch (PrinterException e1) {
			e1.printStackTrace();
			throw new RuntimeException(e1.getMessage());
		}

		// 设置打印类
		printJob.setPageable(book);
		try {
			// 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印
			// boolean a=job.printDialog();
			// if(a)
			// {
			printJob.print();
			// }
		} catch (PrinterException e) {
			e.printStackTrace();
		}

	}
	
	
	

}
